package cn.com.wangluotongxin.merchants.controller;

import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.db.pojo.PageData;
import cn.com.wangluotongxin.merchants.dto.UserDto;
import cn.com.wangluotongxin.merchants.form.UserEditForm;
import cn.com.wangluotongxin.merchants.form.UserForm;
import cn.com.wangluotongxin.merchants.service.IMerchantsUserService;
import cn.com.wangluotongxin.merchants.vo.UserVo;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商户管理-用户表 前端控制器
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-05-17
 */
@Api(tags = "商户管理-用户表 相关接口")
@RestController
@RequestMapping("/user")
public class MerchantsUserController {

    @Autowired
    private IMerchantsUserService userService;

    @ApiOperation("分页")
    @GetMapping("/page")
    public AjaxResult<PageData<UserVo>> getPage(UserDto dto){
        IPage<UserVo> page = userService.getPage(dto);
        return AjaxResult.success(PageData.build(page));
    }

    @ApiOperation("详情")
    @GetMapping("/detail/{id}")
    public AjaxResult<UserVo> detail(@PathVariable("id") Integer id){
        return AjaxResult.success(userService.detail(id));
    }

    @ApiOperation("添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody UserForm form){
        Assert.isTrue(StrUtil.isNotBlank(form.getUsername()), "缺少账号");
        Assert.isTrue(StrUtil.isNotBlank(form.getPassword()), "缺少密码");

        userService.add(form);
        return AjaxResult.success();
    }

    @ApiOperation("编辑")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody UserEditForm form){
        Assert.notNull(form.getId(), "缺少数据ID");
        Assert.isTrue(StrUtil.isNotBlank(form.getUsername()), "缺少账号");

        userService.edit(form);
        return AjaxResult.success();
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Integer id) {
        userService.delete(id);
        return AjaxResult.success();
    }
}